<?php
//最新文章new标识
function timeZone($from){ 
$now = new Typecho_Date(Typecho_Date::gmtTime()); 
return $now->timeStamp - $from < 3*24*60*60 ? true : false; 
} 
//获得读者墙
function getFriendWall()
{
$db = Typecho_Db::get();
$sql = $db->select('COUNT(author) AS cnt', 'author', 'url', 'mail')
->from('table.comments')
->where('status = ?', 'approved')
->where('type = ?', 'comment')
->where('authorId = ?', '0')
->where('mail != ?', '672085897@qq.com')   //排除自己上墙
->group('author')
->order('cnt', Typecho_Db::SORT_DESC)
->limit('25');    //读取几位用户的信息
$result = $db->fetchAll($sql);
 
if (count($result) > 0) {
$maxNum = $result[0]['cnt'];
foreach ($result as $value) {
 $mostactive .= '<li><img alt src="http://1.gravatar.com/avatar/'.md5(strtolower($value['mail'])).'?s=36&d=&r=G" height="48" width="48"></img><a target="_blank" rel="nofollow" href="' . $value['url'] . '" title="' . $value['author'] . '-' . $value['cnt'] . '条评论">' . $value['author'] . '</a><br><strong>' . $value['cnt'] . '条</strong></li>';   
}
echo $mostactive;
}
}
//zidai

	function timesince($older_date,$comment_date = false) {
		$chunks = array(
			array(86400 , 'd'),
			array(3600 , 'h'),
			array(60 , 'm'),
			array(1 , 's'),
		);
		$newer_date = time();
		$since = abs($newer_date - $older_date);
		if($since < 2592000){
			for ($i = 0, $j = count($chunks); $i < $j; $i++){
				$seconds = $chunks[$i][0];
				$name = $chunks[$i][1];
				if (($count = floor($since / $seconds)) != 0) break;
			}
			$output = $count.$name.' ago';
		}else{
			$output = !$comment_date ? (date('Y-m-j G:i', $older_date)) : (date('Y-m-j', $older_date));
		}
		return $output;
	}
	function theme_random_posts(){
        $defaults = array(
            'number' => 8,
            'before' => '<ul class="list">',
            'after' => '</ul>',
            'xformat' => '<li><a href="{permalink}">{title}</a></li>'
        );
        $db = Typecho_Db::get();

        $sql = $db->select()->from('table.contents')
            ->where('status = ?','publish')
            ->where('type = ?', 'post')
            ->limit($defaults['number'])
            ->order('RAND()');
        
        $result = $db->fetchAll($sql);
        echo $defaults['before'];
        foreach($result as $val){
			$val = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($val);
            echo str_replace(array('{permalink}', '{title}'),array($val['permalink'], $val['title']), $defaults['xformat']);
        }
        echo $defaults['after'];
    }
	function theme_most_commented_posts(){
        $defaults = array(
            'number' => 8,
            'before' => '<ul class="list">',
            'after' => '</ul>',
            'xformat' => '<li><a href="{permalink}">{title}<span>({commentsNum})</span></a></li>'
        );
        $time = time() + 8*60*60;
        $db = Typecho_Db::get();

        $sql = $db->select()->from('table.contents')
            ->where('created <= ?', $time)
	    ->where('type = ?', 'post')
            ->limit($defaults['number'])
            ->order('commentsNum',Typecho_Db::SORT_DESC);
        $result = $db->fetchAll($sql);
        echo $defaults['before'];
        foreach($result as $val){
			$val = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($val);
            echo str_replace(array('{permalink}', '{title}', '{commentsNum}'), array($val['permalink'], $val['title'], $val['commentsNum']), $defaults['xformat']);
        }
        echo $defaults['after'];
    }
    function threadedComments($comments,$singleCommentOptions) {
			$imgUrl = Helper::options()->siteUrl . 'usr/themes/Vincent/images/pingback.jpg';
            $author = '<a href="'.$comments->url.'" rel="external nofollow" target="_blank">'.$comments->author.'</a>';
			$depth = $comments->levels +1;
			if($depth<=1){
				$size = (int) Helper::options()->commentsPageSize;
				$curr = (int) $comments->current();
				$cn   = (int) $comments->sequence;
				$n = $cn + ( $curr -1 )*$size;
				$floor = $n.'楼'; // 主楼层
			}else{
				$floor = ($depth-1).'#';  // 子楼层
				$style = 'style="margin-left:' . ceil(80/2) . 'px;"';
			}
        ?>
	<li id="<?php $comments->theId(); ?>" <?php if( $depth == 2) echo $style;?>>
		<div id="comment-<?php $comments->theId(); ?>">
            <div  class="comment-body <?php echo 'depth-'.$depth;?>">
			    <div class="commentmeta <?php echo 'commentmeta-'.$depth;?>"><?php if($comments->type == 'pingback'){ echo '<img src="'.$imgUrl.'" width="40" height="40" />';}else{$comments->gravatar('40','');}?></div>
				<div class="commentmetadata"><span class="c-floor"><?php echo $floor; ?></span></div>
			
				<div class="vcard"><?php echo $author; ?><span class="c-time"><?php $comments->date('Y-m-d') ?></span><span class="c-at"><?php $comments->reply('回复TA') ?></span></div>
				<?php $comments->content(); ?>
            </div>
            <div class="children">
                <?php if ($comments->children) { ?><?php $comments->threadedComments($singleCommentOptions); ?><?php } ?>
            </div>
		</div>
	</li>
<?php } ?>